
Schedule = function(spec) {
    this.before = this.toDate(spec.before);
    this.after = this.toDate(spec.after);
};

Schedule.prototype = {
    type: 'Schedule',

    intersect: function(schedule) {
        return new Schedule({
            before: Date.maximum(this.before, schedule.before),
            after: Date.minimum(this.after, schedule.after)
        });
    },

    empty: function() {
        return this.before && this.after && (this.before.getDate() > this.after.getDate());
    },

    earliestTime: function() {
        return this.after || new Date();
    },

    toDate: function(s) {
        return (typeof s === 'string') ? new Date(Date.parse(s)) : s;
    }
};

Schedule.parse = function(obj) {
    if (obj.type == Schedule.prototype.type) {
        return obj;
    }
    return new Schedule(obj);
};
